package com.zrf.webmagic.pageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * oschina 博客爬虫
 *
 * @author zhouruifeng
 *         create at 2017/3/26.
 */
public class OschinaBlogPageProcesser implements PageProcessor {
    /**
     * 站点
     */
    private Site site = Site.me().setDomain("my.oschina.net");

    /**
     * 处理页面的数据
     *
     * @param page 页面
     */
    public void process(Page page) {
        List<String> links = page.getHtml().links().regex("https://my\\.oschina\\.net/zhouruifeng/blog").all();
        page.addTargetRequests(links);
        page.putField("title", page.getHtml().xpath("//div[@class='list-item']//div[@class='title']//a[@class='blog-title']").toString());
//        page.putField("href", page.getHtml().$("//div[@class='list-item']//div[@class='title']//a[@class='blog-title']")
        page.putField("tags", page.getHtml().xpath("//div[@class='list-item']//div[@class='title']//span[@class='status-tag']").toString());
    }

    public Site getSite() {
        return site;
    }

    /**
     * main
     * @param args 参数
     */
    public static void main(String[] args) {
        Spider.create(new OschinaBlogPageProcesser()).addUrl("http://my.oschina.net/zhouruifeng/blog")
                .addPipeline(new ConsolePipeline()).run();
    }
}
